package com.hsq.socket;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class SocketTCP02Client {
    public static void main(String[] args) throws IOException {
        //1.连接服务端
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("Client socket = " + socket.getClass());
        //得到socket对象关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(outputStream));
        bufferedWriter.write("Hello server 字符流");
        //插入一个换行符，表示结束标志，如果以这个未结束标志则read要使用readLine
        bufferedWriter.newLine();
        bufferedWriter.flush();
//        socket.shutdownOutput();该结束标志也可以
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
        String read = bufferedReader.readLine();
        System.out.println(read);

        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
        System.out.println("Client 退出");
    }
}
